package com.xdtech.parking.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.common.utils.encrypt.DESCoder;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.parking.dao.ParkUserDao;
import com.xdtech.parking.model.ParkUser;
import com.xdtech.parking.service.ParkUserService;
import com.xdtech.parking.service.ParkingDeviceService;
import com.xdtech.parking.util.ParkingContants;
import com.xdtech.parking.vo.BillDetail;
import com.xdtech.parking.vo.ParkUserItem;
import com.xdtech.sys.util.SysConstants;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-21 14:00:53
 * @since 1.0
 * @see
 */
@Service
public class ParkUserServiceImpl implements ParkUserService {
	private Log log = LogFactory.getLog(ParkUserServiceImpl.class);
	@Autowired
	private ParkUserDao parkUserDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private ParkingDeviceService parkingDeviceService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param entity
	 */
	public void save(ParkUser entity) {
		parkUserDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<ParkUser> entities) {
		parkUserDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param entity
	 */
	public void delete(ParkUser entity) {
		parkUserDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		parkUserDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param id
	 * @return
	 */
	public ParkUser get(Long id) {
		return parkUserDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @return
	 */
	public List<ParkUser> getAll() {
		return parkUserDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> parkUsers = null;
		long rows = 0;
		if (pg!=null) {
			Page<ParkUser> page = parkUserDao.findPage(pg,"from ParkUser order by createTime desc", values);
			parkUsers = BeanUtils.copyListProperties(
					ParkUserItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<ParkUser> parkUserList = parkUserDao.find("from ParkUser order by id desc", values);
			parkUsers = BeanUtils.copyListProperties(
					ParkUserItem.class, parkUserList);
			rows = parkUsers.size();
		}
		results.put("total", rows);
		results.put("rows", parkUsers);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateParkUser(ParkUserItem item) {
		ParkUser parkUser = null;
		if (item.getId()==null) {
			parkUser = new ParkUser();
			parkUser.setPassword(DESCoder.encrypt("123456"));
		}else {
			parkUser = parkUserDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(parkUser, item);
		parkUserDao.save(parkUser);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param parkUserId
	 * @return
	 */
	public ParkUserItem loadParkUserItem(Long parkUserId) {
		ParkUser parkUser = parkUserDao.get(parkUserId);
		return ParkUserItem.createItem(parkUser);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteParkUserInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteParkUserInfo(List<Long> parkUserIds) {
		for (Long id : parkUserIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-21 14:00:53
	 * @modified by
	 * @return
	 */
	@Override
	public List<ParkUserItem> loadItems() {
		List<ParkUser> parkUsers = getAll();
		return ParkUserItem.createItems(parkUsers);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-21下午2:34:39
	 * @modified by
	 * @param telephone
	 * @param password
	 * @return
	 */
	@Override
	public ParkUserItem loginCheck(String telephone, String password) {
		ParkUser parkUser = parkUserDao.findUnique("from ParkUser where password=? and telephone=?", DESCoder.encrypt(password),telephone);
		if (parkUser!=null) {
			return ParkUserItem.createItem(parkUser);
		}else {
			return null;
		}
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-21下午2:49:32
	 * @modified by
	 * @param telephone
	 * @return
	 */
	@Override
	public boolean checkTelephone(String telephone) {
		ParkUser parkUser = getParkUserByTelephone(telephone);
		return parkUser==null?false:true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-21下午2:50:13
	 * @modified by
	 * @param item
	 * @return
	 */
	@Override
	public boolean saveOrUpdateUser(ParkUserItem item) {
		ParkUser parkUser = getParkUserByTelephone(item.getTelephone());
		if (parkUser!=null) {
			//性别
			parkUser.setSex(item.getSex());
			//用户名
			parkUser.setName(item.getName());
			save(parkUser);
			return true;
		}else {
			return false;
		}
		
	}
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-6-21下午4:10:21
	 * @modified by
	 * @param item
	 * @return
	 */
	private ParkUser getParkUserByTelephone(String telephone) {
		return parkUserDao.findUnique("from ParkUser where telephone=?", telephone);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-21下午2:50:52
	 * @modified by
	 * @param telephone
	 * @param password
	 * @return
	 */
	@Override
	public ParkUserItem register(String telephone, String password,String type) {
		ParkUser parkUser = new ParkUser();
		parkUser.setTelephone(telephone);
		//密码加密
		parkUser.setPassword(DESCoder.encrypt(password));
		if (StringUtils.isBlank(type)) {
			//默认设置类型
			parkUser.setType(ParkingContants.PARK_COMMON_USER_TYPE);
		}else {
			parkUser.setType(type);
		}
		
		parkUser.setStatus(SysConstants.USER_ENABLE_STATUS);
		save(parkUser);
		return ParkUserItem.createItem(parkUser);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-21下午4:08:44
	 * @modified by
	 * @param telephone
	 * @param oldPwd
	 * @param newPwd
	 * @return
	 */
	@Override
	public String updatePwd(String telephone, String oldPwd, String newPwd) {
		String rs = "";
		ParkUser parkUser = getParkUserByTelephone(telephone);
		if (parkUser!=null) {
			String oldEncyPwd = DESCoder.encrypt(oldPwd);
			if (!oldEncyPwd.equals(parkUser.getPassword())) {
				//旧密码不一致
				rs = "旧密码验证不对.";
			}else {
				parkUser.setPassword(DESCoder.encrypt(newPwd));
				save(parkUser);
			}
		}else {
			rs = "用户不存在.";
		}
		return rs;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-27下午10:42:05
	 * @modified by
	 * @param parkNo
	 * @return
	 */
	@Override
	public BillDetail getBillDetail(String aid,String parkNo) {
		//费用计算
		BillDetail billDetail = parkingDeviceService.loadBillInfo(aid,parkNo);
		
		return billDetail;
	}

}
